package com.cx.bank.view;

import com.cx.bank.factory.UserDaoFactory;
import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.model.UserBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @author jxau_scf
 * @date 2021/7/19 - 16:35
 */
public class BankView extends JFrame {
    private JButton register, login, codeChange;
    private JLabel idLabel, codeLabel, bank;
    public JTextField idText;
    private JPasswordField codeTest;

    public static UserBean getUserBean() {
        return userBean;
    }

    public static void setUserBean(UserBean userBean) {
        BankView.userBean = userBean;
    }

    private static UserBean userBean = null;

    public BankView(){
        building();
        addlisteners();
    }

    private void building() {
        setLayout(null);
        //标签
        bank = new JLabel("bank");
        idLabel = new JLabel("账号");
        codeLabel = new JLabel("密码");
        //按钮
        register = new JButton("注册");
        login = new JButton("登录");
        codeChange = new JButton("修改，忘记密码");

        idText = new JTextField(20);
        codeTest = new JPasswordField(20);
        bank.setBounds(250,20,120,50);
        Font font = new Font("圆体", Font.BOLD, 50);
        Font font1 = new Font("圆体", Font.BOLD, 30);
        bank.setFont(font);

        register.setBounds(0, 500, 120, 25);
        login.setBounds(225, 450, 120, 25);
        codeChange.setBounds(450, 500, 120, 25);

        idLabel.setBounds(150, 300, 120, 25);
        idLabel.setFont(font1);
        codeLabel.setBounds(150, 375, 120, 25);
        codeLabel.setFont(font1);

        idText.setBounds(250, 300, 200, 30);
        codeTest.setBounds(250, 375, 200, 30);

        add(bank);
        add(idLabel);
        add(idText);
        add(codeLabel);
        add(codeTest);
        add(login);
        add(register);
        add(codeChange);

        setBounds(600,600,600,600);
        setLocation((2000-getWidth())/2,(1000-getHeight())/2);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private void addlisteners(){
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("注册用户信息");
                //关闭当前页面
                BankView.this.dispose();
                //打开注册页面
                new BankRegisterView().setVisible(true);

            }
        });

        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int flag = 0;
                String userName = idText.getText();
                String password = codeTest.getText();
                if (!userName.equals("") && !password.equals("")){
                    UserDaoFactory userDaoFactory = null;
                    try {
                        userDaoFactory = UserDaoFactory.getInstance();
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException ex) {
                        ex.printStackTrace();
                    }
                    ManagerImpl manager = userDaoFactory.getManager();


                    try {
                        boolean loginStatus = manager.login(userName, Integer.valueOf(password));
                        if (loginStatus){
                            userBean = manager.getBankDao().findUser(userName, Integer.valueOf(password));
                            //如果登录成功，关闭当前页面
                            BankView.this.dispose();
                            //打开IndexView
                            new IndexView().setVisible(true);
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    //关闭当前页面
//                    BankView.this.dispose();
//                    new IndexView().setVisible(true);
                }
            }
        });

        codeChange.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(codeChange, "此功能暂未开放", "标题", JOptionPane.WARNING_MESSAGE);
            }
        });
    }
    public static void main(String[] args) {
        BankView bankView = new BankView();
        bankView.show();
    }


}
